from django.db import models

# Create your models here.
from django.contrib.auth.models import User


class Customer(models.Model):
    """客户信息表"""
    name = models.CharField(max_length=32)
    qq = models.CharField(max_length=64, unique=True)
    weixin = models.CharField(max_length=64, blank=True, null=True)
    age = models.PositiveSmallIntegerField(blank=True, null=True)
    gender = models.SmallIntegerField(choices=((0, "Female"), (1, 'Male')), blank=True, null=True)
    phone = models.PositiveIntegerField(blank=True, null=True)
    source_choices = ((0, "百度商桥"),
                      (1, "51CTO"),
                      (2, "QQ群"),
                      (3, "知乎"),
                      (4, "搜狗"),
                      (5, "转介绍"),
                      (6, "其他"),
                      )
    source = models.SmallIntegerField(choices=source_choices)
    referral_from = models.ForeignKey('self', models.CASCADE, 'my_referrals', verbose_name="转介绍",
                                      blank=True, null=True)  # 转介绍，自关联
    consult_courses = models.ManyToManyField('Course', verbose_name="咨询的课程")
    status_choices = ((0, "已报名"),
                      (1, "未报名"),
                      (2, "已退学"),
                      )
    status = models.SmallIntegerField(choices=status_choices)
    consultant = models.ForeignKey('UserProfile', models.CASCADE, verbose_name="课程顾问")
    consult_content = models.TextField(max_length=1024)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "客户信息"
        verbose_name_plural = "客户信息表"


class Enrollment(models.Model):
    """注册登记表（学员报名表）：存每个学生报名了哪些课程"""
    customer = models.ForeignKey('Customer', models.CASCADE)
    class_grade = models.ForeignKey('ClassList', models.CASCADE)
    enrollment_date = models.DateField()

    def __str__(self):
        return '%s' % self.customer

    class Meta:
        unique_together = ('customer', 'class_grade')
        verbose_name = "注册登记"
        verbose_name_plural = "注册登记表"


class FollowUpRecord(models.Model):
    """跟进记录表"""
    customer = models.ForeignKey('Customer', models.CASCADE)
    content = models.TextField(max_length=1024)
    status_choices = (
        (0, "近期无报名计划"),
        (1, "一个月内报名"),
        (2, "2周内报名"),
        (3, "已报名其他机构"),
    )
    status = models.SmallIntegerField(choices=status_choices)
    consultant = models.ForeignKey('UserProfile', models.CASCADE, verbose_name="课程顾问")
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s' % self.customer

    class Meta:
        verbose_name = "跟进记录"
        verbose_name_plural = "跟进记录表"


class Course(models.Model):
    """课程表"""
    name = models.CharField(max_length=64, unique=True)
    price = models.PositiveIntegerField(default=19800)
    outline = models.TextField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "课程"
        verbose_name_plural = "课程表"


class ClassList(models.Model):
    """班级列表"""
    course = models.ForeignKey('Course', models.CASCADE)
    semester = models.PositiveSmallIntegerField("学期")
    branch = models.ForeignKey('Branch', models.CASCADE)
    class_type_choices = ((0, "脱产班"), (1, "周末班"), (2, "网络班"))
    class_type = models.SmallIntegerField(choices=class_type_choices)
    teachers = models.ManyToManyField('UserProfile')
    start_date = models.DateField()
    end_date = models.DateField()

    def __str__(self):
        # return self.course  # 如果这里不返回字符串，前端就会报错。django的Admin也一样报错
        return '%s' % self.course

    class Meta:
        verbose_name = "班级"
        verbose_name_plural = "班级列表"


class CourseRecord(models.Model):
    """上课记录表：每一节课生成一条上课记录"""
    class_grade = models.ForeignKey('ClassList', models.CASCADE)
    day_number = models.PositiveSmallIntegerField(verbose_name="节次")
    teacher = models.ForeignKey('UserProfile', models.CASCADE)
    course_content = models.TextField("课程内容", max_length=1024)
    has_homework = models.BooleanField(default=True)
    homework_title = models.CharField(max_length=128, blank=True, null=True)
    homework_requirement = models.TextField("作业需求", max_length=1024, blank=True, null=True)

    def __str__(self):
        return '%s day:%s' % (self.class_grade, self.day_number)

    class Meta:
        unique_together = ('class_grade', "day_number")
        verbose_name = "上课记录"
        verbose_name_plural = "上课记录表"


class StudyRecord(models.Model):
    """学习记录表：每个学生上的每一节课的记录和成绩"""
    course_record = models.ForeignKey('CourseRecord', models.CASCADE)
    student = models.ForeignKey('Enrollment', models.CASCADE)
    score_choices = ((100, 'A+'),
                     (90, 'A'),
                     (85, 'B+'),
                     (80, 'B'),
                     (75, 'B-'),
                     (70, 'C+'),
                     (60, 'C'),
                     (50, 'C-'),
                     (40, 'D'),
                     (10, 'D'),
                     (0, 'N/A'),
                     )
    score = models.SmallIntegerField(choices=score_choices)
    show_status_choices = ((0, "缺勤"),
                           (1, "已签到"),
                           (2, "迟到"),
                           (3, "请假"),
                           )
    grade_comment = models.TextField("成绩批注", max_length=1024, blank=True, null=True)

    def __str__(self):
        return '%s:%s' % (self.student, self.course_record)

    class Meta:
        unique_together = ('student', "course_record")
        verbose_name = "学习记录"
        verbose_name_plural = "学习记录表"


class UserProfile(models.Model):
    """账户信息"""
    user = models.OneToOneField(User, models.CASCADE)
    name = models.CharField(max_length=32)
    roles = models.ManyToManyField('Role', blank=True)

    def __str__(self):
        return self.name


class Role(models.Model):
    """角色表"""
    name = models.CharField(max_length=32, unique=True)
    menus = models.ManyToManyField('Menu')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "角色"
        verbose_name_plural = "角色表"


class Branch(models.Model):
    """分校"""
    name = models.CharField(max_length=128, unique=True)

    def __str__(self):
        return self.name


class Menu(models.Model):
    """动态菜单"""
    name = models.CharField(max_length=32, unique=True)
    url_type = models.SmallIntegerField(choices=((0, '相对url'), (1, '绝对url')))
    url_name = models.CharField(max_length=128, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "动态菜单"
        verbose_name_plural = "动态菜单表"




